import { observable, action } from "mobx";
import api from '../../apis/app';
import { Toast, Tools } from 'weaver-mobile';
import { Status } from '../../components/examine'
import { getRequest } from '../../common/getRequest';

const getLabel = Tools.getLabel;

export default class ExamineStore {
  @observable hasInit = true;
  @observable status = '0'; // 审核状态
  @observable loading = false;
  @observable data = {};
  @observable id = '';

  @action init = () => {
    this.hasInit = false;

    this.id = getRequest().id;

    api.getApplyInfo(this.id).then(res => {
      this.data = res;
      this.status = res.status;
      this.hasInit = true;
    }).catch(res => {
      this.hasInit = true;
    });
  }

  @action submit = status => {
    this.loading = true;

    api.processApply({ id: this.id, status: status }).then(() => {
      this.status = status;
      this.loading = false;

      const content = status === Status.AGREE ? getLabel(523025, '已同意') : getLabel(523023, '已拒绝');

      Toast.success(content, 1);
    }).catch(res => {
      const { api_errormsg } = res;

      this.loading = false;
      Toast.fail(api_errormsg, 1);
    });
  }
}